﻿package com.life.view
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import com.life.view.brick.ExpandBrick;
	import com.life.view.brick.HardBrick;
	import com.life.view.brick.SubstractBrick;
	import com.life.view.brick.SuperHardBrick;
	import com.life.vo.BallVO;
	import com.life.vo.BonusVO;
	import com.life.vo.BrickVO;
	import com.life.vo.PadVO;

	public class GameLayer extends Sprite
	{
		private const RESTRICT_TOP : int = 0;
		private const RESTRICT_LEFT : int = 0;
		private const RESTRICT_RIGHT : int = 400;
		private const RESTRICT_BOTTOM : int = 400;
		
		private var ballView : BallView;
		private var padView : PadView;
		private var brickViewList : Array = new Array( );
		private var dropItemViewList : Array = new Array( );
		
		public function GameLayer()
		{
			super( );
			init( );
		} 
		
		private function init( ) : void {
			initBallView( );
			initBricksView( );
			initPadView( );
			initListener( );
		}
		
		private function initBallView( ) : void {
			ballView = new BallView( );
			var tmpBallVO : BallVO = new BallVO( );
			tmpBallVO.posX = 100;
			tmpBallVO.posY = 300;
			tmpBallVO.speedY = -5;
			tmpBallVO.speedX = 2;
			ballView.ballVO = tmpBallVO;
			ballView.updatePos( );
			addChild(ballView);
		}
		
		private function initBricksView( ) : void {			
			for(var i : int = 0; i < 50; i++) {
				var tmpVO : BrickVO = new BrickVO( );
				tmpVO.posX = 50 + i % 10 * 40;
				tmpVO.posY = 50 + int(i / 10) * 30;
				tmpVO.type = Math.floor(5 * Math.random());
				var tmpView : BaseBrickView = getBrick(tmpVO.type);
				tmpView.brickVO = tmpVO;				
				tmpView.updatePos( );
				tmpView.updateGraph( );
				brickViewList.push(tmpView);
				addChild(tmpView);
			}			
		}
		
		private function initPadView( ) : void {
			padView = new PadView( );
			var tmpPadVO : PadVO = new PadVO( );
			tmpPadVO.posX = 200;
			tmpPadVO.posY = 300;
			padView.padVO = tmpPadVO;
			padView.updatePos( );
			padView.updateGraph( );
			addChild(padView);	
		}
		
		private function initListener( ) : void {
			addEventListener(Event.ENTER_FRAME, efHandler);
		}
		
		private function efHandler(event : Event) : void {
			movePad( );		
			moveBall( );
			moveBonus( );
			checkBallHit( );
		} 
		
		private function movePad( ) : void {
			padView.padVO.posX = mouseX;
			padView.updatePos( );	
		}
		
		private function moveBall( ) : void {
			var tmpBallVO : BallVO = ballView.ballVO;			
			tmpBallVO.posX += tmpBallVO.speedX;
			tmpBallVO.posY += tmpBallVO.speedY;
			ballView.updatePos( );
		}
		
		private function moveBonus( ) : void {
			for each(var elem : BonusView in dropItemViewList) {
				elem.bonusVO.posY += elem.bonusVO.dropSpeed; 
				elem.updatePos( );
			}
		}
		
		private function checkBallHit( ) : void {
			//检测砖块碰撞
			checkBrickHit( );
			//检测小板碰撞
			checkPadHit( );
			//检测边界碰撞
			checkEdgeHit(ballView.ballVO);
			//检测是否吃到奖励
			checkBonusHit( );
		}
		
		private function checkBrickHit( ) : void {
			for(var i : int = 0; i < brickViewList.length; i++) {
				if(brickViewList[i].hitTestObject(ballView)){
					//改变速度
					ballView.ballVO.speedY *= -1;
					(brickViewList[i] as BaseBrickView).brickVO.hitCount++;
					//移除砖块
					if(checkBrickToRemove(brickViewList[i].brickVO)){
						
						dropBonusItem(brickViewList[i].brickVO);
						//移除		
						removeChild(brickViewList[i]);
						brickViewList.splice(i,1);
						i--;						
					}else{
						//不移除则更新样式
						(brickViewList[i] as BaseBrickView).updateGraph( );
					}	
				}
			}
		}
		
		private function checkPadHit( ) : void {
			if(ballView.hitTestObject(padView)){
				ballView.ballVO.speedY = -ballView.ballVO.speedY;
				//改变水平坐标的碰撞
				ballView.ballVO.speedX = (ballView.ballVO.posX - padView.padVO.posX) / 5;
			}
		}
		
		private function checkEdgeHit(tmpBallVO : BallVO) : void {
			if(tmpBallVO.posX > RESTRICT_RIGHT || tmpBallVO.posX < RESTRICT_LEFT){
				tmpBallVO.speedX = -tmpBallVO.speedX;
			}
			if(tmpBallVO.posY > RESTRICT_BOTTOM || tmpBallVO.posY < RESTRICT_TOP) {
				tmpBallVO.speedY = -tmpBallVO.speedY;
			}
		}
		
		private function checkBonusHit( ) : void {
			for(var i : int = 0; i < dropItemViewList.length; i++) {
				if(dropItemViewList[i].hitTestObject(padView)){
					//移除砖块
					//触发碰撞特效
					doHitEffect(dropItemViewList[i].bonusVO.type);		
					//移除		
					removeChild(dropItemViewList[i]);
					dropItemViewList.splice(i,1);
					i--;
				}
			}
		}
		
		private function dropBonusItem(brickVO : BrickVO) : void {
			var tmpBonusView : BonusView;
			var tmpBonusVO : BonusVO  = new BonusVO( );
			switch(brickVO.type){
				case BrickVO.EXPAND: tmpBonusVO.type = BonusVO.EXPAND; break;
				case BrickVO.SUBSTRACT: tmpBonusVO.type = BonusVO.SUBSTRACT; break;
			}
			switch(brickVO.type){
				case BrickVO.EXPAND:
				case BrickVO.SUBSTRACT:
					tmpBonusVO.posX = brickVO.posX;
					tmpBonusVO.posY = brickVO.posY;
					tmpBonusView = new BonusView( );	
					tmpBonusView.bonusVO = tmpBonusVO;
					tmpBonusView.updateGraphic();
					dropItemViewList.push(tmpBonusView);
					addChild(tmpBonusView);
					break;	
				default:					
					break;
			}
		}
		
		private function doHitEffect(type : int) : void {
			switch(type){
				case BonusVO.EXPAND:
					padView.padVO.status = PadVO.STATUS_EXPAND;
					padView.updateGraph( );
					break;
				case BonusVO.SUBSTRACT:
					padView.padVO.status = PadVO.STATUS_SUBSTRACT;
					padView.updateGraph( );
					break;
			}
		}
		
		private function checkBrickToRemove(brickVO : BrickVO) : Boolean {
			switch(brickVO.type){
				case BrickVO.NORMAL:
				case BrickVO.SUBSTRACT:
				case BrickVO.EXPAND:
					if(brickVO.hitCount >= 1) return true;
					break;
				case BrickVO.HARD:	
					if(brickVO.hitCount >= 2) return true;
					break;
				case BrickVO.SUPER_HARD:	
					if(brickVO.hitCount >= 3) return true;
					break;
			}
			return false;
		}
		
		/**
		 * 按砖块类型返还相应实例
		 */ 
		private function getBrick(type : int) : BaseBrickView {
			switch(type){
				case BrickVO.EXPAND:
					return new ExpandBrick( );
					break;
				case BrickVO.HARD:
					return new HardBrick( );
					break;
				case BrickVO.SUBSTRACT:
					return new SubstractBrick( );
					break;
				case BrickVO.SUPER_HARD:
					return new SuperHardBrick( );
					break;
				default:
					return new BaseBrickView( );
					break
			}
		}
			
	}
}